<?php

class Empeval_Notifications_Accept {
	
	private $config;
	private $enabled;
	private $type;
	
	public function __construct($type) {
		$this->type = $type;
		$this->config = Empeval_Config::getInstance()->notifications->$type;
		$this->enabled = (bool)$this->config->enabled;
	}
	
	public function addNotifications($bgfId, $toSuperior) {
		if (!$this->enabled)
				return;
		
		$tplCode = $this->getTplCode($toSuperior);
		$tpl = EmpevalNotificationTemplatesQuery::create()->findOneByName($tplCode);
		if (!($tpl instanceof EmpevalNotificationTemplates))
			throw new Empeval_Exception_App(Empeval_Exception_Codes::ERROR_TPL_NOT_EXIST, array($tplCode));
		
		$bonusGoalsForm = EmpevalViewBgfWithSuperiorPositionIdQuery::create()
			->useUserPositionsRelatedBySuperiorUserPositionIdQuery('SuperiorUserPosition')
				->joinUsers('Superior')
			->endUse()
			->useUserPositionsRelatedByUserPositionIdQuery('UserPosition')
				->joinUsers('Employee')
			->endUse()
			->with('SuperiorUserPosition')
			->with('UserPosition')
			->with('Employee')
			->with('Superior')
			->findOneById($bgfId);
		
		if (!($bonusGoalsForm instanceof EmpevalViewBgfWithSuperiorPositionId))
			throw new Empeval_Exception_App(Empeval_Exception_Codes::ERROR_NOTIFICATION_SEND);
		
		$employee = $bonusGoalsForm->getUserPositionsRelatedByUserPositionId()->getUsers();
		$superior = $bonusGoalsForm->getUserPositionsRelatedBySuperiorUserPositionId()->getUsers();
		
		$contentText = $this->addContentData($tpl->getContentText(), $employee, $superior, $bonusGoalsForm);
		$contentHtml = $this->addContentData($tpl->getContentHtml(), $employee, $superior, $bonusGoalsForm);
		
		$con = Propel::getConnection('vgp_empeval');
		$con->beginTransaction();
		try {
			$notification = new EmpevalNotifications();
			$notification->setStatus(EmpevalNotifications::STATUS_TO_SEND);
			$notification->setNotificationTemplates($tpl);
			$notification->save($con);
			
			$notificationData = new EmpevalNotificationData();
			$notificationData->setSubject(($toSuperior) ? $this->getSubject($tpl->getSubject(), $employee) : $tpl->getSubject());
			$notificationData->setContentText($contentText);
			$notificationData->setContentHtml($contentHtml);
			$notificationData->setNotifications($notification);
			$notificationData->save($con);

			$notificationUser = new EmpevalNotificationUsers();
			$notificationUser->setUsers(($toSuperior) ? $superior : $employee);
			$notificationUser->setNotifications($notification);
			$notificationUser->setEmail(($toSuperior) ? $superior->getEmail() : $employee->getEmail());
			$notificationUser->save($con);
			
			$con->commit();
		} catch (Exception $e) {
			$con->rollBack();
			throw $e;
		}
	}
	
	private function getTplCode($toSuperior) {
		switch ($this->type) {
			case 'toaccept': return ($toSuperior) ? EmpevalNotificationTemplates::TO_SUPERIOR_ACCEPT : EmpevalNotificationTemplates::TO_EMPLOYEE_ACCEPT;
			case 'updated': return ($toSuperior) ? EmpevalNotificationTemplates::UPDATED_TO_SUPERIOR : EmpevalNotificationTemplates::UPDATED_TO_EMPLOYEE;
			case 'notaccepted': return ($toSuperior) ? EmpevalNotificationTemplates::NOT_ACCEPTED_TO_SUPERIOR : EmpevalNotificationTemplates::NOT_ACCEPTED_TO_EMPLOYEE;
		}
	}
	
	private function getSubject($subject, EmpevalUsers $employee) {
		$subject = str_replace('{$e_fname}', $employee->getFname(), $subject);
		$subject = str_replace('{$e_lname}', $employee->getLname(), $subject);
		return $subject;
	}
	
	private function addContentData($content, EmpevalUsers $employee, EmpevalUsers $superior, EmpevalViewBgfWithSuperiorPositionId $bonusGoalsForm) {
		$content = str_replace('{$s_fname}', $superior->getFname(), $content);
		$content = str_replace('{$s_lname}', $superior->getLname(), $content);
		$content = str_replace('{$e_fname}', $employee->getFname(), $content);
		$content = str_replace('{$e_lname}', $employee->getLname(), $content);
		$content = str_replace('{$link}', Empeval_Config::getInstance()->baseUrl . sprintf($this->config->data->bonusGoalsFormUrl, $bonusGoalsForm->getId()), $content);
		return $content;
	}
	
}